package com.example.e_book.Adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.e_book.Activity.ReadActivity;
import com.example.e_book.DAO.BookshelfDAO;
import com.example.e_book.R;
import com.example.e_book.entity.Book;
import com.example.e_book.entity.User;

import java.util.List;

public class BookAdapter extends RecyclerView.Adapter<BookAdapter.BookViewHolder> {

    private List<Book> bookList;
    private Context context;
    private User user;

    public BookAdapter(List<Book> bookList, Context context, User user) {
        this.bookList = bookList;
        this.context = context;
        this.user = user;
    }

    @NonNull
    @Override
    public BookViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_book, parent, false);
        return new BookViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull BookViewHolder holder, int position) {
        Book book = bookList.get(position);
        holder.titleTextView.setText(book.getTitle());
        holder.authorTextView.setText("作者：" + book.getAuthor());
        holder.categoryTextView.setText("类别：" + book.getCategory());
        String coverImagePath = "file:///android_asset/" + book.getCoverImagePath();
        Glide.with(holder.itemView.getContext()).load(coverImagePath).into(holder.coverImageView);
        holder.itemView.setOnClickListener(v -> {
            Intent intent = new Intent(holder.itemView.getContext(), ReadActivity.class);
            intent.putExtra("CONTENT_PATH", book.getContentPath());
            holder.itemView.getContext().startActivity(intent);
        });
        holder.addbutton.setOnClickListener(v -> {
            int bookId = book.getBookId();
            int userId = user.getId();
            BookshelfDAO bookshelfDAO = new BookshelfDAO(context, user);
            long result = bookshelfDAO.addBookToShelf(bookId, userId);
            if (result == -1) {
                Toast.makeText(context, "加入书架失败", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(context, "加入书架成功", Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public int getItemCount() {
        return bookList.size();
    }

    public void updateList(List<Book> newBookList) {
        this.bookList = newBookList;
    }

    public static class BookViewHolder extends RecyclerView.ViewHolder {
        ImageView coverImageView;
        TextView titleTextView;
        TextView authorTextView;
        TextView categoryTextView;
        Button addbutton;

        public BookViewHolder(View itemView) {
            super(itemView);
            coverImageView = itemView.findViewById(R.id.coverImageView);
            titleTextView = itemView.findViewById(R.id.titleTextView);
            authorTextView = itemView.findViewById(R.id.authorTextView);
            categoryTextView = itemView.findViewById(R.id.categoryTextView);
            addbutton = itemView.findViewById(R.id.addbutton);
        }
    }
}
